package dao;

import entity.Meeting;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

public class MeetingDao extends BaseDao{
    /**
     * 会议搜索+分页
     * @param startIndex
     * @param pageSize
     * @param title
     * @param status
     * @return
     */
    public List<Meeting> findPage(Integer startIndex, Integer pageSize, String title, String status) {

        String sql;
        Object[] args= {};

        if("-1".equals(status)){
            sql="select * from meeting where title like ? order by publish_date desc limit ?,?";
            args = new Object[]{"%"+title+"%",startIndex,pageSize};
        }else {
            sql = "select * from meeting where title like ? and status = ? order by publish_date desc limit ?,?";
            args = new Object[]{"%"+title+"%",status,startIndex,pageSize};
        }

        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Meeting.class),args);
    }

    /**
     * 根据标题和会议状态查询符合记录的条数
     * @param title
     * @param status
     * @return
     */
    public Integer countByTitleAndStatus(String title, String status) {
        String sql;
        Object[] args= {};

        if("-1".equals(status)){
            sql ="select count(1) from meeting where title like ? ";
            args = new Object[]{"%"+title+"%"};
        }else {
            sql="select count(1) from meeting where title like ? and status = ? ";
            args = new Object[]{"%"+title+"%",status};
        }
        return jdbcTemplate.queryForObject(sql,Integer.class,args);

    }

    public void save(Meeting meeting) {
        jdbcTemplate.update("insert into meeting values(null,?,?,?,?,?,?,?,?,?)",
        meeting.getDeptName(),
        meeting.getDeptId(),
        meeting.getTitle(),
        meeting.getContent(),
        meeting.getPublishDate(),
        meeting.getStartTime(),
        meeting.getEndTime(),
        meeting.getStatus(),
        meeting.getMakeUser()
        );
    }

    /**
     *查询会议详情
     * @param meetingId
     * @return
     */
    public Meeting findById(Long meetingId) {
          return jdbcTemplate.queryForObject("select * from meeting where id = ?",new BeanPropertyRowMapper<>(Meeting.class),meetingId);
    }

    /**
     *根据会议id查询该会议实到人数
     * @param meetingId
     * @return
     */
    public Integer countByMeetingId(Long meetingId) {
        return jdbcTemplate.queryForObject("select count(1) from meeting_join where m_id = ?",Integer.class,meetingId);
    }

    /**
     *判断指定用户是否参加了指定会议
     * @param userId
     * @param meetingId
     * @return
     */
    public Integer countByUserIdAndMeetingId(Long userId, Long meetingId) {
          return jdbcTemplate.queryForObject("select count(1) from meeting_join where u_id=? and m_id = ?",Integer.class,userId,meetingId);
    }

    /**
     * 参加会议
     * @param userId
     * @param meetingId
     */
    public void joinMeeting(Long userId, Long meetingId) {
        jdbcTemplate.update("insert into meeting join values(?,?)",userId,meetingId);
    }

    /**
     * 取消会议
     * @param userId
     * @param meetingId
     */
    public void unJoinMeeting(Long userId, Long meetingId) {
        jdbcTemplate.update("delete from meeting_join where u_id =? and m_id = ?",userId,meetingId);
    }
}
